
#import "DoctorViewController.h"
#import "Masonry.h"
#import "ChooseViewController.h"
#import "DisViewController.h"
#import "EruptyViewController.h"
#import "CommonViewController.h"
#import "FocusDoctorViewController.h"

#define viewBGColor [UIColor colorWithWhite:0.887 alpha:1.000]
#define margin 15

typedef NS_ENUM(NSInteger, createType)
{
    createLabel = 0,
    createButton,
};

typedef NS_ENUM(NSInteger, btnViewType)
{
    btnViewFirstType = 0,
    btnViewSecondType,
};

@interface DoctorViewController ()

@property (nonatomic,weak) UILabel *nameLabel;
@property (nonatomic,weak) UILabel *diseaseType;

@property (nonatomic,weak) UIView *diseaseView;
@property (nonatomic,weak) UIView *eruptyView;
@property (nonatomic,weak) UIView *ifConfirmedView;
@property (nonatomic,weak) UIView *ifTreatedView;

@property (nonatomic,weak) UIView *treatedView;

@property (nonatomic,weak) UILabel *btnFirstViewLabel;
@property (nonatomic,weak) UILabel *btnSecondViewLabel;

@property (nonatomic,weak) UIButton *confirmedBtn;
@property (nonatomic,weak) UIButton *notConfirmedBtn;

@property (nonatomic,weak) UIButton *treatedBtn;
@property (nonatomic,weak) UIButton *neverTreatedBtn;

@property (nonatomic,weak) UIButton *lastClickBtn;

@property (nonatomic,weak) UILabel *returnedLabel;

@end

@implementation DoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置用户信息
    [self creatIllMessage];
    
    [self createSelectionViews];
    
    [self createApplyView];
}

#pragma make --- 设置用户信息
-(void) creatIllMessage {
    
    //设置用户信息
    UILabel *nameLabel = [self getLabelWithText:@"用户信息:\t黑吗" color:kThemeTextColor ];
    self.nameLabel = nameLabel;
    [self.view addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(20);
    }];
    
    //设置疾病类型
    UILabel * diseasLable = [self getLabelWithText:[NSString stringWithFormat:@"疾病类型:\t%@",self.navigationItem.title] color:kThemeTextColor];
    self.diseaseType = diseasLable;
    [self.view addSubview:diseasLable];
    
    [diseasLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(nameLabel);
        make.top.equalTo(nameLabel.bottom).offset(margin);
    }];
    
}

#pragma mark --- 前两个选择栏
-(void) createSelectionViews {
    
    // 创建 diseaseView 栏
    UIView *diseaseView = [self createCommonViewConstrainedFrom:self.diseaseType createType:createLabel viewType:0 placeHolder:@"请选择疾病细分" action:@selector(pushPage:) colTag:1];
    self.diseaseView = diseaseView;
    
    // 创建 EruptyView 栏
    UIView *eruptyView = [self createCommonViewConstrainedFrom:diseaseView createType:createLabel viewType:0 placeHolder:@"请选择并发症(可多选)" action:@selector(pushPage:) colTag:2];
    self.eruptyView = eruptyView;
    
    // 创建 ifConfirmedView 栏
    UIView *ifConfirmedView = [self createCommonViewConstrainedFrom:eruptyView createType:createButton viewType:btnViewFirstType placeHolder:@"是否确诊" action:@selector(btnFirstViewClick:) colTag:0];
    self.ifConfirmedView = ifConfirmedView;
    
    //创建 ifTreatedView 栏
    UIView *ifTreatedView = [self createCommonViewConstrainedFrom:ifConfirmedView createType:createButton viewType:btnViewSecondType placeHolder:@"是否接受过治疗" action:@selector(btnSecondViewClick:) colTag:0];
    self.ifTreatedView = ifTreatedView;
    
}

-(void)createApplyView {
    
    UILabel *doctorLabel = [self getLabelWithText:@"为您匹配到: 42个医生" color:kThemeTextColor];
    [self.view addSubview:doctorLabel];
    
    [doctorLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.ifTreatedView.bottom).offset(margin*7);
    }];
    
    UIImageView *doctorImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_person.png"]];
    [self.view addSubview:doctorImgView];
    
    [doctorImgView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(doctorLabel);
        make.width.height.equalTo(45);
        make.top.equalTo(doctorLabel.bottom).offset(margin*1.5);
    }];
    
    UIButton *applyBtn = [[UIButton alloc]init];
    [applyBtn setTitle:@"就医申请" forState:UIControlStateNormal];
    [applyBtn.titleLabel setFont:BOLDFONT(16)];
    applyBtn.backgroundColor = kThemeColor;
    applyBtn.layer.cornerRadius = 5;
    applyBtn.clipsToBounds = YES;
    [applyBtn addTarget:self action:@selector(applyBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:applyBtn];
    
    [applyBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(doctorImgView);
        make.top.equalTo(doctorImgView.bottom).offset(margin*1.5);
        make.left.equalTo(self.view).offset(100);
        make.right.equalTo(self.view).offset(-100);
        make.height.equalTo(40);
    }];
    
}

-(void)insertCommonViewContentWithView:(UIView *)view labelText:(NSString *)labelText viewType:(btnViewType)viewType action:(SEL)action {
    
    UILabel *label = [self getLabelWithText:labelText color:kThemeTextGray];
    [view addSubview:label];
    
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(20);
        make.centerY.equalTo(view);
    }];
    
    NSString *leftBtnText, *rightBtnText;
    
    if (viewType == btnViewFirstType) {
        leftBtnText = @"已确诊", rightBtnText = @"疑似症状";
        self.btnFirstViewLabel = label;
    } else {
        leftBtnText = @"接受过", rightBtnText = @"未接受过";
        self.btnSecondViewLabel = label;
    }
    
    // 第一个 btn
    UIButton *leftBtn = [self getButtonWithText:leftBtnText];
    [leftBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftBtn];
    
    [leftBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view).offset(45);
        make.centerY.equalTo(view);
        make.width.equalTo(70);
        make.height.equalTo(view);
    }];
    
    UIButton *rightBtn = [self getButtonWithText:rightBtnText];
    [rightBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setSelected:YES];
    [view addSubview:rightBtn];
    
    [rightBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftBtn.right);
        make.centerY.equalTo(view);
        make.width.equalTo(85);
        make.height.equalTo(view);
    }];
    
    if (viewType == btnViewFirstType) {
        self.confirmedBtn = leftBtn, self.notConfirmedBtn = rightBtn;
    } else if (viewType == btnViewSecondType) {
        self.treatedBtn = leftBtn, self.neverTreatedBtn = rightBtn;
    }
    
}

-(void)insertCommonViewContentWithView:(UIView *)view Placeholder:(NSString *)placeholder action:(SEL)action {
    
    UILabel *label = [self getLabelWithText:placeholder color:kThemeTextGray];
    [view addSubview:label];
    
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).offset(20);
        make.centerY.equalTo(view);
    }];
    
    //设置箭头的图片
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_button_input_blue.png"]];
    imageView.userInteractionEnabled = YES;
    [view addSubview:imageView];
    
    [imageView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view).offset(-20);
        make.centerY.equalTo(view);
    }];
    
    UIImageView * imageViewLine = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [view addSubview:imageViewLine];
    
    [imageViewLine makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(imageView.left).offset(-5);
        make.centerY.equalTo(view);
    }];
    
    UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
    [view addGestureRecognizer:recognizer];
    
}

-(void)btnFirstViewClick:(UIButton *)sender {

    [self setSelectedsenderBtn:sender leftBtn:self.confirmedBtn rightBtn:self.notConfirmedBtn changeLabel:self.btnFirstViewLabel needExtraViews:NO];
    
}

-(void)btnSecondViewClick:(UIButton *)sender {

    [self setSelectedsenderBtn:sender leftBtn:self.treatedBtn rightBtn:self.neverTreatedBtn changeLabel:self.btnSecondViewLabel needExtraViews:YES];
}

-(void)setSelectedsenderBtn:(UIButton *)sender leftBtn:(UIButton *)leftBtn rightBtn:(UIButton *)rightBtn changeLabel:(UILabel *)changeLabel needExtraViews:(BOOL)needExtraViews {
    
    self.lastClickBtn = sender;
    
    if (rightBtn == self.lastClickBtn) {
        [leftBtn setSelected:NO];
        [rightBtn setSelected:YES];
        
        changeLabel.text = rightBtn.titleLabel.text;
        
        if (needExtraViews) {
            if (self.ifTreatedView) {
                [self.treatedView removeFromSuperview];
            }
        }
        
    } else {
        [leftBtn setSelected:YES];
        [rightBtn setSelected:NO];
        
        changeLabel.text = leftBtn.titleLabel.text;
        
        if (needExtraViews) {
            UIView *treatedView = [self createCommonViewConstrainedFrom:self.ifTreatedView createType:createLabel viewType:0 placeHolder:@"请选择治疗方式" action:@selector(pushPage:) colTag:3];
            self.treatedView = treatedView;
        }
    }
    
    changeLabel.font = BOLDFONT(14);
    changeLabel.textColor = kThemeColor;
}

//响应疾病细分事件
-(void)pushPage:(UITapGestureRecognizer *)tap {
    
    UILabel *tempLabel;
    
    for (UIView *v in tap.view.subviews) {
        if ([v isKindOfClass:[UILabel class]]) {
            tempLabel = (UILabel *)v;
            break;
        }
    }
    
    CommonViewController *controller;

    switch (tap.view.tag) {
        case 1:
            controller = (DisViewController*)[[DisViewController alloc]init];
            break;
        case 2:
            controller = (EruptyViewController*)[[EruptyViewController alloc]init];
            break;
        case 3:
            controller = (ChooseViewController*)[[ChooseViewController alloc]init];
            break;
    }

    [controller returnText:^(NSString *str) {
        tempLabel.textColor = kThemeTextColor;
        tempLabel.text = str;
    }];
    [self.navigationController pushViewController:controller animated:YES];
 
}

-(void)applyBtn {

    FocusDoctorViewController * fvc = [[FocusDoctorViewController alloc]init];
    
    [self.navigationController pushViewController:fvc animated:YES];
}

-(UILabel *)getLabelWithText:(NSString *)text color:(UIColor *)color {
    
    UILabel *label = [[UILabel alloc]init];
    label.text = text;
    label.textColor = color;
    label.font = kFONT15;
    
    return label;
}

-(UIButton *)getButtonWithText:(NSString *)text {
    
    UIButton *btn = [[UIButton alloc]init];
    
    [btn setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    [btn setTitle:text forState:UIControlStateNormal];
    [btn.titleLabel setFont:kFONT12];
    [btn setTitleColor:kThemeTextColor forState:UIControlStateNormal];
    
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    
    return  btn;
}

-(UIView *) createCommonViewConstrainedFrom:(UIView *)obj createType:(createType)createType viewType:(btnViewType)viewType placeHolder:(NSString *)placeHolder action:(SEL)action colTag:(NSInteger)colTag {
    
    UIView *cView = [[UIView alloc]init];
    cView.backgroundColor = viewBGColor;
    cView.layer.cornerRadius = 5;
    cView.clipsToBounds = YES;
    cView.tag = colTag;
    [self.view addSubview:cView];
    
    if (createType == createLabel) {
        cView.userInteractionEnabled = YES;
    }
    
    [cView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.nameLabel);
        make.top.equalTo(obj.bottom).offset(margin);
        make.height.equalTo(35);
    }];
    
    if (createType == createLabel) {
        [self insertCommonViewContentWithView:cView Placeholder:placeHolder action:action];
    } else if (createType == createButton) {
        if (viewType == btnViewFirstType) {
            [self insertCommonViewContentWithView:cView labelText:placeHolder viewType:btnViewFirstType action:action];
        } else if (viewType == btnViewSecondType) {
            [self insertCommonViewContentWithView:cView labelText:placeHolder viewType:btnViewSecondType action:action];
        }
    }
    
    return cView;
}

@end
